import http from '@/utils/request';
import { DeviceQuery, DeviceVO, DeviceForm } from './types';
import { AxiosPromise } from 'axios';
import type { CheckStockItem, OrderBo } from './types';

// 查询设备列表
export const getDeviceList = (query: DeviceQuery): AxiosPromise<DeviceVO[]> => {
    return http.get('/vending/api/base/list', query);
};

// 查询设备详细
export const getDeviceDetail = (id: number, type: number): AxiosPromise<DeviceVO> => {
    return http.get('/vending/api/base/' + id + '/' + type);
};

// 新增设备
export const createDevice = (data: DeviceForm): AxiosPromise<DeviceVO> => {
    return http.post('/vending/api/base', data);
};

// 修改设备
export const updateDevice = (data: DeviceForm): AxiosPromise<DeviceVO> => {
    return http.put('/vending/api/base', data);
};

// 删除设备
export const deleteDevice = (id: number | Array<number>) => {
    return http.delete('/vending/api/base/' + id);
};

// 导出设备
export const exportDevice = (query: DeviceQuery) => {
    return http.get('/vending/base/export', query);
};

// 更新设备数量
export const updateCount = (id: number, count: number) => {
    return http.get('/vending/api/base/updateCount/' + id + '/' + count);
};

// 获取设备列表
export function getlistDevice(query: DeviceQuery) {
    return http.get('/vending/api/base/listDevice', query);
}

// 获取设备总数
export const getDeviceTotal = () => {
    return http.get('/vending/api/base/total');
};

// 获取在线设备数
export const getDeviceOnline = () => {
    return http.get('/vending/api/base/online');
};

// 通过设备编号获取设备信息
export const getInfoByNo = (deviceNo: string) => {
    return http.get('/vending/api/base/ByDeviceNo/' + deviceNo);
};

//设备库存分页查询
export const getDeviceStockPage = (params: {
    pageNum: number
    pageSize: number
    deviceNo?: string
}) => {
    return http.get('/vending/api/stock/list', params);
};

// 获取有库存的设备
export const getDeviceWithStock = (productId: number) => {
    return http.get(`/vending/api/stock/getDevice/${productId}`);
};

// 检查库存并创建订单
export const checkStock = (checkStockList: CheckStockItem[], orderBo: OrderBo) => {
    return http.post('/vending/api/stock/checkStock', {
        checkStockList,
        orderBo
    });
};